package com.softeng.network.chess;

import javax.swing.JFrame;
//import javax.swing.JOptionPane;

public class Pawn extends ChessPiece{
	
	public boolean firstMove;
	
	public boolean twoSquaresOnFirst;
	
	public boolean validMove; 
		
	//-----------------------------------------------------------------------------------------------	
	
	Pawn(String color){
		
		idString = "pawn";
		firstMove = true;
		this.color = color;
		twoSquaresOnFirst = false;
		
		if (color == "white"){
			this.pieceString = "resources/assets/pawn_white.gif";
		}else{
			this.pieceString = "resources/assets/pawn_black.gif";
		}
		
	}
	
	//-----------------------------------------------------------------------------------------------
	
	Pawn(String color, int position, JFrame window){
		
		this(color); 
		
		addPieceImage(position, window, this.pieceString); 		
	
	}
	
	//-----------------------------------------------------------------------------------------------
	
	// Sees if a particular move is a move that a Pawn is allowed to make
	public boolean validMove(int currentPosition, int position, ChessPiece[] boardArray){
		
		boolean validMove = false;
		
		int currentRow = getRowFromPos(currentPosition);
		int currentCol = getColFromPos(currentPosition);
		int nextRow = getRowFromPos(position);
		int nextCol = getColFromPos(position);
		
		//bottom player moving up the board
		if(boardArray[currentPosition].color.equals(Chess.getInstance(false).myColor)){

			if(this.firstMove == true){			
				//if first move is 1 space forward
				//if(nextRow == (currentRow + 1) && currentCol == nextCol && !boardArray[position].color.equals("black")){					
				if(nextRow == (currentRow + 1) && currentCol == nextCol && boardArray[position]==null){					
					validMove = true;
					System.out.println("first move, moved 1 space forward");
					System.out.println("");
					this.firstMove = false; 
				}
				
				//if first move is 2 spaces forward
				else if(nextRow == (currentRow + 2) && currentCol == nextCol && boardArray[position]==null){
					validMove = true;
					System.out.println("first move, moved 2 spaces forward");
					System.out.println("");
					this.firstMove = false;  
					this.twoSquaresOnFirst = true;
				}
				
				//if moving diagonal to take a piece
				else if((nextRow == (currentRow + 1) && nextCol == (currentCol + 1) && boardArray[position]!=null) || (nextRow == (currentRow + 1) && nextCol == (currentCol - 1) && boardArray[position]!=null)){
					validMove = true; 
					System.out.println("taking a piece on first move");
					System.out.println("");
					this.firstMove = false;
				}
				
			}
			
			//if the pawn has already moved once
			else{
				//if moving forward 1 space
				if(nextRow == (currentRow + 1) && boardArray[position]==null && currentCol == nextCol){
					
						validMove = true; 
						System.out.println("not first move, moving forward one space");
						System.out.println("");
						this.firstMove = false;
						this.twoSquaresOnFirst = false; //shows that the pawn has moved since its double square first move
					
				}
				//if moving diagonal to take a piece
				else if(nextRow == (currentRow + 1) && (nextCol == (currentCol + 1) || nextCol == (currentCol - 1)) && boardArray[position] != null){
					
						validMove = true; 
						System.out.println("taking a piece, not first move");
						System.out.println("");
						this.firstMove = false;
						this.twoSquaresOnFirst = false; //shows that the pawn has moved since its double square first move
					
				}
				//en passant movement
				else if(nextRow == (currentRow + 1) && (nextCol == (currentCol + 1) || nextCol == (currentCol - 1)) && boardArray[position] == null){
				
					if (boardArray[currentPosition + 1] != null && boardArray[currentPosition + 1].idString.equals("pawn") && ((Pawn)boardArray[currentPosition + 1]).twoSquaresOnFirst == true && position == (currentPosition - 7)){
						validMove = true; 
						System.out.println("taking a piece, en passant up and right");
						System.out.println("");
						this.firstMove = false;
					}
					else if (boardArray[currentPosition - 1] != null && boardArray[currentPosition - 1].idString.equals("pawn") && ((Pawn)boardArray[currentPosition - 1]).twoSquaresOnFirst == true && position == (currentPosition - 9)){
						validMove = true; 
						System.out.println("taking a piece, en passant up and left");
						System.out.println("");
						this.firstMove = false;
					}
					
				}
				
			}
			//---------------------------------------------------------------------------
			
			
		}
		//top player moving down the board
		if(boardArray[currentPosition].color.equals(Chess.getInstance(false).oppColor)){

			if(this.firstMove == true){			
				//if first move is 1 space forward
				if(nextRow == (currentRow - 1) && currentCol == nextCol && boardArray[position]==null){					
					validMove = true;
					System.out.println("first move, moved 1 space forward");
					System.out.println("");
					this.firstMove = false; 	
				}
				
				//if first move is 2 spaces forward
				else if(nextRow == (currentRow - 2) && currentCol == nextCol && boardArray[position]==null){
					validMove = true;
					System.out.println("first move, moved 2 spaces forward");
					System.out.println("");
					this.firstMove = false;  
					this.twoSquaresOnFirst = true;
				}
				
				//if moving diagonal to take a piece
				else if((nextRow == (currentRow - 1) && nextCol == (currentCol + 1) && boardArray[position]!=null) || (nextRow == (currentRow - 1) && nextCol == (currentCol - 1) && boardArray[position]!=null)){
		
						validMove = true; 
						System.out.println("taking a piece on first move");
						System.out.println("");
						this.firstMove = false;
					
				}
				
			}
			
			//if the pawn has already moved once
			else{
				
				//if moving forward 1 space
				if(nextRow == (currentRow - 1) && boardArray[position]==null && currentCol == nextCol){
		
						validMove = true; 
						System.out.println("not first move, moving forward one space");
						System.out.println("");
						this.firstMove = false;
					
				}
				
				//if moving diagonal to take a piece
				else if(nextRow == (currentRow - 1) && (nextCol == (currentCol + 1) || nextCol == (currentCol - 1)) && boardArray[position]!=null){
					
						validMove = true; 
						System.out.println("taking a piece, not first move");
						System.out.println("");
						this.firstMove = false;
					
				}
				//en passant movement
				else if(nextRow == (currentRow - 1) && (nextCol == (currentCol + 1) || nextCol == (currentCol - 1)) && boardArray[position] == null){
				
					if (boardArray[currentPosition + 1] != null && boardArray[currentPosition + 1].idString.equals("pawn") && ((Pawn)boardArray[currentPosition + 1]).twoSquaresOnFirst == true && position == (currentPosition + 9)){
						validMove = true; 
						System.out.println("taking a piece, en passant down and right");
						System.out.println("");
						this.firstMove = false;
					}
					else if (boardArray[currentPosition - 1] != null && boardArray[currentPosition - 1].idString.equals("pawn") && ((Pawn)boardArray[currentPosition - 1]).twoSquaresOnFirst == true && position == (currentPosition + 7)){
						validMove = true; 
						System.out.println("taking a piece, en passant down and left");
						System.out.println("");
						this.firstMove = false;
					}
					
				}
				
			}
			//---------------------------------------------------------------------------
			
			
		}
		return validMove; 
		
	}
	
}
